const Service = require('node-windows').Service;
const path = require('path');

const svc = new Service({
  name: 'digitalBackup',
  description: '运行在windows上的数据中间层服务',
  script: path.join(__dirname, 'app.js'),
  nodeOptions: [
    '--harmony',
    '--max_old_space_size=4096'
  ],
  // 可选添加：日志和环境变量
  logs: {
    error: path.join(__dirname, 'logs/error.log'),
    output: path.join(__dirname, 'logs/output.log')
  },
  env: [{ name: "NODE_ENV", value: "production" }]
});

// ===== 事件处理器 =====
svc.on('install', () => {
  console.log('Service installed successfully!');
  svc.start();
});

svc.on('start', () => {
  console.log('Service started. PID:', svc.pid);
});

// 关键错误处理
svc.on('error', (err) => {
  console.error('SERVICE ERROR:', err.message);
});

// 防止重复安装
svc.on('alreadyinstalled', () => {
  console.warn('Service is already installed');
});

// ===== 安装执行 =====
try {
  svc.install();
} catch (installErr) {
  console.error('INSTALLATION FAILED:', installErr);
}
